<template>
  <div>
    <el-card class="box_card">
      <el-button type="primary" @click="$router.push('/import')"
        >excel导入</el-button
      >
      <el-button type="success" @click="exportToExcel">excel导出</el-button>
    </el-card>
    <el-card class="card">
      <el-table :data="state.tableData" border style="width: 100%">
        <el-table-column type="index" label="#" />
        <el-table-column prop="username" label="姓名" />
        <el-table-column prop="mobile" label="联系方式" />
        <el-table-column prop="avatar" label="头像">
          <template #default="{ row }">
            <img :src="row.avatar" alt="" />
          </template>
        </el-table-column>
        <el-table-column label="角色" prop="role[0].title" />
        <el-table-column label="开通时间">
          <template #default="{ row }">
            <span>{{ days(parseInt(row.openTime)).format("YYYY-MM-DD") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template #default="scope">
            <el-button type="primary" size="small">查看</el-button>
            <el-button type="infor" size="small">角色</el-button>
            <el-button type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        v-model:currentPage="state.query.page"
        v-model:page-size="state.query.size"
        :page-sizes="[2, 5, 10, 15]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="state.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup>
import Export from "@/components/introduction";
import { getUser } from "@/http/api";
import { ref, reactive, onMounted } from "vue";

const days = require("dayjs");

const state = reactive({
  tableData: [],
  query: {
    page: 1,
    size: 5,
  },
  total: 0,
});
//导出
const exportToExcel = () => {
  let fields = {
    username: "姓名",
    mobile: "联系方式",
    avatar: "头像",
    title: "角色",
    openTime: "开通时间",
  };
  let data = JSON.parse(JSON.stringify(state.tableData));
  Export(data, fields, "测试");
};
//获取数据
const tableData = async () => {
  const res = await getUser(state.query);
  state.tableData = res.data.list;
  state.total = res.data.total;
  console.log(state.tableData, state.total);
};
//分页
const handleSizeChange = (val) => {
  console.log(`${val} items per page`);
  state.query.size = val;
  tableData();
};
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`);
  state.query.page = val;
  tableData();
};

onMounted(() => {
  tableData();
});
</script>

<style scoped lang="scss">
.box_card {
  margin: 20px;
  height: 80px;
}
.card {
  margin: 20px;
  img {
    width: 60px;
    height: 60px;
  }
  .el-button {
    padding: 5px 8px;
  }
}
</style>
